<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/03/22 12:54:00 $)
// $RCSfile: modernbillv5.login.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Initialization function. You can connect to your database etc over here.
*/
function loginShareInit()
{
	global $loginshare;

	$loginshare->moduleloaded = true;
}

/**
* Authorize a user based on email and password
*/
function loginShareAuthorize($username, $password)
{
	global $dbCore, $_SWIFT, $loginshare, $settings, $CONF;

	if (defined("ISMODERNBILLV5"))
	{
		$_user = $CONF["user"];
		if (empty($_user["clientContactID"]))
		{
			return false;
		}

	} else {
		$_loginshare = $settings->getSection("loginshare");
		$dbLoginShare = new dbCore($_loginshare["mbhostname5"], $_loginshare["mbdbuser5"], $_loginshare["mbdbpass5"], $_loginshare["mbdbname5"], "mysql", false);

		$_user = $dbLoginShare->queryFetch("SELECT * FROM `client_contacts` WHERE `contact_email` = '". $dbCore->escape($username) ."' OR `contact_username` = '". $dbCore->escape($username) ."';");
		if (empty($_user["client_contact_id"]))
		{
			return false;
		}
	}
	if (!empty($_user["sessionID"]) && !empty($_user["username"]) && defined("ISMODERNBILLV5"))
	{
		$userid = getLoginShareUser(LOGINAPI_MODERNBILLV5, $_user["clientContactID"]);
		if (!$userid)
		{
			// Not registered, Register him
			$userid = insertUser(true, $_user["email"], substr(buildHash(),0,6), $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_MODERNBILLV5, $_user["clientContactID"], $_user["firstName"]." ".$_user["lastName"], $_SWIFT["tgroup"]["languageid"], 0, false, 1, true, true);
		}

		if (!$userid)
		{
			// ======= GET THE USER WITH THE EMAIL AND TAG HIM UNDER MODERNBILL =======
			$_swiftuser = $dbCore->queryFetch("SELECT `userid` FROM `". TABLE_PREFIX ."useremails` WHERE `email` = '". $dbCore->escape($_user["email"]) ."' LIMIT 1;");
			if (!empty($_swiftuser["userid"]))
			{
				$dbCore->query("UPDATE `". TABLE_PREFIX ."users` SET `loginapi_moduleid` = '". LOGINAPI_MODERNBILLV5 ."', `loginapi_userid` = '". intval($_user["clientContactID"]) ."' WHERE `userid` = '". intval($_swiftuser["userid"]) ."';");

				$userid = $_swiftuser["userid"];
			} else {
				return false;
			}
		}

		$_swiftuser = $loginshare->loadSWIFTUser($userid);
		if (!$_swiftuser)
		{
			return false;
		}

		$_SWIFT["user"] = $_swiftuser;

		return $_swiftuser["userid"];
	} else if (!empty($_user["client_id"]) && !empty($_user["contact_email"]) && !defined("ISMODERNBILLV5") && $_user["contact_password"] == sha1(sha1($password)) && !empty($_user["contact_password"]) && !empty($password)) {

		$userid = getLoginShareUser(LOGINAPI_MODERNBILLV5, $_user["client_contact_id"]);
		if (!$userid)
		{
			// Not registered, Register him
			$userid = insertUser(true, $_user["contact_email"], substr(buildHash(),0,6), $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_MODERNBILLV5, $_user["client_contact_id"], $_user["contact_first_name"]." ".$_user["contact_last_name"], $_SWIFT["tgroup"]["languageid"], 0, false, 1, true, true);
		}

		if (!$userid)
		{
			// ======= GET THE USER WITH THE EMAIL AND TAG HIM UNDER MODERNBILL =======
			$_swiftuser = $dbCore->queryFetch("SELECT `userid` FROM `". TABLE_PREFIX ."useremails` WHERE `email` = '". $dbCore->escape($_user["contact_email"]) ."' LIMIT 1;");
			if (!empty($_swiftuser["userid"]))
			{
				$dbCore->query("UPDATE `". TABLE_PREFIX ."users` SET `loginapi_moduleid` = '". LOGINAPI_MODERNBILLV5 ."', `loginapi_userid` = '". intval($_user["client_contact_id"]) ."' WHERE `userid` = '". intval($_swiftuser["userid"]) ."';");

				$userid = $_swiftuser["userid"];
			} else {
				return false;
			}
		}

		$_swiftuser = $loginshare->loadSWIFTUser($userid);
		if (!$_swiftuser)
		{
			return false;
		}

		$_SWIFT["user"] = $_swiftuser;

		return $_swiftuser["userid"];
	}

	return false;
}

/**
* Return the Unique User ID of the current user
*/
function loginShareUserID()
{
	global $_SWIFT;

	if (empty($_SWIFT["user"]["userid"]))
	{
		return false;
	} else {
		return $_SWIFT["user"]["userid"];
	}
}

/**
* Logout the current user
*/
function loginShareLogout()
{
	global $session, $_SWIFT;

	$session->updateSession($_SWIFT["session"]["sessionid"], 0);

	return true;
}

/**
* Load the user credentials into current workspace. The following variables should be declared for proper working:
* userid - User id that is set in the "users" table
* fullname
* email - Array
* password (MD5 Hashed)
* usergroupid - If this is not set, then it will use the default registered user group for this template group
*/
function loginShareLoadUser()
{
	global $dbCore, $_SWIFT, $loginshare;

	if (empty($_SWIFT["session"]["typeid"]))
	{
		$_SWIFT["user"]["loggedin"] = false;
		return false;
	}

	$_user = $loginshare->loadSWIFTUser($_SWIFT["session"]["typeid"]);
	if (!$_user)
	{
		$_SWIFT["user"]["loggedin"] = false;

		return false;
	}

	$_SWIFT["user"] = $_user;

	return true;
}

/**
* Renders the Login Share Form
*/
function renderLoginShareForm()
{
	global $_SWIFT;

	$forms = array();

	$forms[0]["title"] = $_SWIFT["language"]["hostname"];
	$forms[0]["name"] = "mbhostname5";
	$forms[1]["title"] = $_SWIFT["language"]["dbname"];
	$forms[1]["name"] = "mbdbname5";
	$forms[2]["title"] = $_SWIFT["language"]["dbuser"];
	$forms[2]["name"] = "mbdbuser5";
	$forms[3]["title"] = $_SWIFT["language"]["dbpass"];
	$forms[3]["name"] = "mbdbpass5";

	return $forms;

}
?>